package com.xiaomiMall.mange.service.Impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.xiaomiMall.mange.domain.MallLoginBody;
import com.xiaomiMall.mange.domain.MallLoginUser;
import com.xiaomiMall.mange.domain.MallUser;
import com.xiaomiMall.mange.mapper.MallUserMapper;
import com.xiaomiMall.mange.service.MallLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class MallLoginServiceImpl implements MallLoginService {

    @Resource
    MallUserMapper mallUserMapper;

    @Autowired
    private MallTokenServiceImpl mallTokenService;

    @Override
    public String checkPassword(MallLoginBody mallLoginBody) {
        MallUser mallUser = mallUserMapper.selectMallUserByEmail(mallLoginBody.getEmail());
        MallLoginUser mallLoginUser = new MallLoginUser();
        if (StringUtils.isNull(mallUser)) {
            return null;
        } else if (SecurityUtils.matchesPassword(mallLoginBody.getPassword(),mallUser.getPassword())) {
            mallLoginUser.setUserId(mallUser.getId());
            mallLoginUser.setMallUser(mallUser);
            String token = mallTokenService.createToken(mallLoginUser);
            return token;
        }
        return null;
    }
}
